package com.ygxsk.carhome.views;

import android.text.InputFilter;
import android.text.Spanned;
import android.view.Gravity;

import com.blankj.utilcode.util.ToastUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Created by itchenqi on 2019/1/25 0025.
 * description：文本的表情输入限制内容
 */
public class EmojiFilter implements InputFilter {

    //关键的正则表达式
    Pattern emoji = Pattern.compile("[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\ud83e\udd00-\ud83e\udfff]|[\u2600-\u27ff]", Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        Matcher emojiMatcher = emoji.matcher(source);

        //禁止特殊符号

//                String speChat = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~！@#￥%……&*（）——+|{}【】‘；：”“’。，、？]";
//                Pattern pattern = Pattern.compile(speChat);
//                Matcher matcher = pattern.matcher(source.toString());

        //原文链接：https://blog.csdn.net/qq_36488374/article/details/83856714

        //禁止输入空格
        if (source.equals(" ") || source.toString().contentEquals("\n")) {
            //LogUtils.d("itchen--输入的是空格");
            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0);
            ToastUtils.showShort("不支持空格~");

            return "";

            //禁止输入表情
        } else if (emojiMatcher.find()) {

            ToastUtils.make().setGravity(Gravity.CENTER, 0, 0);
            ToastUtils.showShort("不支持表情符号输入~");
            //Toast.makeText(mContext, "不支持Emoji输入", Toast.LENGTH_SHORT).show();
            return "";
        }

        //原文链接：https://blog.csdn.net/qq_36488374/article/details/83856714
        return null;
    }

}